<template>
  <div class="app-container">
    <div class="add-btn cursor" @click="handelAdd" v-has="'version:add'">新增</div>
    <top-title :updateTime="true" :content="content"></top-title>
    <div class="search-group">
      <div class="input-wrap">
        <div class="label">项目名称:</div>
        <div class="input-item">
          <a-input :maxLength="30" clearable v-model="queryParam.name" placeholder="请输入单位名称" />
        </div>
      </div>

      <div class="input-wrap">
        <div class="label">英文名称:</div>
        <div class="input-item">
          <a-input :maxLength="30" clearable v-model="queryParam.enName" placeholder="请输入责任民警姓名" />
        </div>
      </div>

      <div class="btn-wrap">
        <a-button class="searchBtn" @click="searchQuery()" type="primary"> 搜索 </a-button>
        <a-button class="resetBtn" @click="searchReset()" type="primary"> 重置 </a-button>
      </div>
    </div>

    <div>
      <a-table
        rowKey="id"
        class="tableList"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        @change="handleTableChange"
      >
        <template slot="projectDescribe" slot-scope="records">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{ records }} </span>
            </template>
            <span>{{ records }} </span>
          </a-tooltip>
        </template>
        <template v-slot:action="records">
          <span class="operation deatil-color" @click="jumpTargetPage(records)" v-has="'version:detail'">详情</span>
          <span class="operation deatil-color" @click="showDownload(records)" v-has="'version:download'">下载</span>
          <span class="operation deatil-color" @click="apkHandle(records)" v-has="'version:download'">二维码</span>
        </template>
      </a-table>
    </div>

    <add-modal ref="addmodal" @refresh="searchQuery()"></add-modal>
    <apk-qr-code ref="apkQrCode" :title="titleQr" :urlText="urlTextQr"></apk-qr-code>
  </div>
</template>

<script>
import { uploadVersion } from '@/api/operate'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import topTitle from '@/components/topTitle'
import apkQrCode from '@/components/apkQrCode/apkQrCode'
import addModal from './modules/versionAddModal'
import { comMethods } from '@/mixins/comMethods'

export default {
  name: '',
  components: { topTitle, apkQrCode, addModal },
  mixins: [JeecgListMixin, comMethods],
  data() {
    return {
      content: [['版本控制']],
      url: {
        list: '/sys/pProjectVersion/list',
      },
      titleQr: '',
      urlTextQr: '',
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 100,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '项目名称',
          dataIndex: 'NAME',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '英文名称',
          dataIndex: 'enName',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '版本号',
          dataIndex: 'versionCode',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '生效版本名称',
          dataIndex: 'version',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '项目描述',
          dataIndex: 'projectDescribe',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'projectDescribe' },
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'action' },
          align: 'center',
        },
      ],
    }
  },
  mounted() {},
  methods: {
    jumpTargetPage(records) {
      this.$router.push({ path: '/isystem/custom/modules/versionControlDetail', query: { id: records.projectId } })
    },
    handelAdd() {
      this.$refs.addmodal.show()
    },
    showDownload(records) {
      uploadVersion({ id: records.projectId }).then((res) => {
        if (res.code == 200) {
          let urlName = res.result.url
          urlName = urlName.substring(urlName.lastIndexOf('oneGirl/') + 1)
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = this.imgPrefix + res.result.url
          link.setAttribute('download', urlName)
          document.body.appendChild(link)
          link.click()
        }
      })
    },
    apkHandle(records) {
      this.titleQr = `扫描下方二维码即可下载${records.version}版本的apk安装包`
      this.urlTextQr = this.imgPrefix + records.url
      this.$refs.apkQrCode.show()
    },
  },
  destroy() {},
}
</script>

<style scoped lang="less">
.app-container {
  background: var(--theme-content-bg);
  position: relative;
}
</style>
